package com.example.leetcode.solution;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Solution12 {

    public static String intToRoman(int num) {
        if (num >= 1 && num <= 3999) {
            Map<Integer, String> romanMap = new HashMap<>();
            romanMap.put(1, "I");
            romanMap.put(5, "V");
            romanMap.put(50, "L");
            romanMap.put(500, "D");
            Map<Integer, String> romanMap2 = new HashMap<>();
            romanMap2.put(10, "X");
            romanMap2.put(100, "C");
            romanMap2.put(1000, "M");
            romanMap.putAll(romanMap2);

            StringBuffer result = new StringBuffer();
            while (num >= 10) {
                List<Integer> keys = romanMap2.keySet().stream().sorted().collect(Collectors.toList());
                for (int i = keys.size() - 1; i >= 0; i--) {
                    int temp = num / keys.get(i);
                    int n = temp * keys.get(i);
                    if (temp != 0) {
                        String c = romanMap.get(n);
                        if (c == null) {
                            c = "";
                            String c1 = romanMap.get(keys.get(i));
                            if (n == 40) {
                                c = "XL";
                            } else if (n == 400 ) {
                                c = "CD";
                            } else if(n == 90){
                                c = "X" + romanMap.get(keys.get(i) * 10);
                            } else if(n == 900){
                                c = "C" + romanMap.get(keys.get(i) * 10);
                            }else {
                                if (temp == 4) {
                                    c = "IV" + c1;
                                } else if (temp == 9) {
                                    c = "IX" + c1;
                                } else {
                                    if (temp < 4) {
                                        for (int k = 0; k < temp; k++) {
                                            c += c1;
                                        }
                                    } else {
                                        c = romanMap.get(keys.get(i) * 5);
                                        for (int k = 1; k < temp - 5; k++) {
                                            c += romanMap.get(keys.get(i));
                                        }
                                        c += c1;
                                    }
                                }
                            }
                        }
                        result.append(c) ;
                        num = num - temp * keys.get(i);
                        if (num < 10) {
                            break;
                        }
                    }
                }
            }
            if (num == 4) {
                result.append("IV") ;
            } else if (num == 9) {
                result.append("IX");
            } else {
                if (num < 4) {
                    for (int k = 0; k < num; k++) {
                        result.append( "I");
                    }
                } else {
                    result.append("V");
                    for (int k = 0; k < num - 5; k++) {
                        result.append("I");
                    }
                }
            }
            return result.toString();
        }
        return "";
    }
}
